package com.yunao.web.controller;

import com.yunao.commons.enums.ResultEnum;
import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.entity.Function;
import com.yunao.entity.User;
import com.yunao.service.FunctionService;
import com.yunao.service.UserService;
import com.yunao.web.annotation.UserLoginToken;
import com.yunao.web.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user/function/")
@Slf4j
public class FunctionController {
    @Autowired
    private UserService userService;
    @Autowired
    private FunctionService functionService;

    /**
     * showdoc
     *
     * @param
     * @return {code: 0,msg: "成功",data:""}
     * @catalog API文档/用户模块
     * @title 当前组织功能栏列表
     * @description 查看当前组织功能栏列表
     * @method get
     * @url /user/function/myselfOrgan
     * @remark
     * @number 01
     */
    @UserLoginToken
    @GetMapping("/myselfOrgan")
    public ResultVO myselfOrgan(){
        User userInfo =  TokenUtil.getTokenUser(userService);
        Integer organId = userInfo.getOrganId();

        return ResultVOUtil.success(findByOrgan(organId));
    }

    /**
     * showdoc
     *
     * @param organId 必选 int 组织ID
     * @return {code: 0,msg: "成功",data:""}
     * @catalog API文档/用户模块
     * @title 组织功能
     * @description 查看指定组织功能栏列表
     * @method get
     * @url /user/function/organ
     * @remark
     * @number 02
     */
    @UserLoginToken
    @GetMapping("/organ")
    public ResultVO organ(@RequestParam(name = "organId",required = true) Integer organId){
        return ResultVOUtil.success(findByOrgan(organId));
    }

    /**
     * showdoc
     *
     * @param
     * @return {code: 0,msg: "成功",data:""}
     * @catalog API文档/用户模块
     * @title 当前用户功能
     * @description 查看当前用户功能
     * @method get
     * @url /user/function/myselfUser
     * @remark
     * @number 03
     */
    @UserLoginToken
    @GetMapping("/myselfUser")
    public ResultVO myselfUser(){
        User userInfo =  TokenUtil.getTokenUser(userService);
        Integer userId = userInfo.getUserId();

        return ResultVOUtil.success(findByUser(userId));
    }

    /**
     * showdoc
     *
     * @param userId 必选 int 用户ID
     * @return {code: 0,msg: "成功",data:""}
     * @catalog API文档/用户模块
     * @title 用户功能
     * @description 查看指定用户功能
     * @method get
     * @url /user/function/user
     * @remark
     * @number 04
     */
    @UserLoginToken
    @GetMapping("/user")
    public ResultVO user(@RequestParam(name = "userId",required = true) Integer userId){
        return ResultVOUtil.success(findByUser(userId));
    }

    /**
     * showdoc
     *
     * @param function 必选 Function 功能工具表单
     * @return {code: 0,msg: "成功",data:""}
     * @catalog API文档/用户模块
     * @title 修改功能工具
     * @description 修改指定功能工具
     * @method get
     * @url /user/function/update
     * @remark
     * @number 05
     */
    @UserLoginToken
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public ResultVO updateOne(Function function){
        Function obj = functionService.findById(function.getId());
        if(function.getId()!=null&&!function.getId().toString().trim().equals("")&&obj!=null){
            functionService.updateOne(function);
            return ResultVOUtil.success();
        }else {
            return ResultVOUtil.error(ResultEnum.FUNCTIONS_NULL);
        }

    }

    private Function findByOrgan(Integer organId){
        Function function= functionService.findByOrganId(organId);
        if(function==null){
            function = new Function();
            function.setOrganId(organId);
            function.setType(2);
            functionService.InsertOne(function);
        }
        return function;
    }

    private Function findByUser(Integer userId){
        Function function= functionService.findByUserId(userId);
        if(function==null){
            User one = userService.findOne(userId);
            Integer organId = one.getOrganId();
            function = new Function();
            function.setUserId(userId);
            function.setType(0);
            functionService.InsertOne(function);
        }
        return function;
    }
}
